<template>
  <div class="app-container">
    <el-card shadow="never">
      <!-- 面包屑需要从学科进入的时候才会显示 -->
      <!-- 需要判断从学科点击过来 携带的参数是否有效 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="$route.query.id&&$route.query.name">
      <el-breadcrumb-item :to="{ path: '/subjects/list' }">学科管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
      <el-breadcrumb-item>目录管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 顶部搜索清除模块,新增模块 -->
    <el-divider v-if="$route.query.id&&$route.query.name"></el-divider>
      <el-form :model="subjectInfo" :inline="true" class="demo-form-inline" ref="filterForm">
        <el-form-item label="目录名称" prop="tagName">
          <el-input style="width: 200px" class="filter-item" v-model="subjectInfo.tagName" ></el-input>
        </el-form-item>

        <el-form-item label="状态" class="el-status" prop="state">
          <el-select v-model="subjectInfo.state" placeholder="请选择">
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" size="small" @click="deleteSearch">清除</el-button>
        </el-form-item>

        <el-form-item>
          <el-button class="filter-item" size="small" type="primary"
            @click="searchSubjectInfo">搜索</el-button
          >
        </el-form-item>

        <el-button
          class="filter-item fr"
          size="small"
          style="margin-left: 10px"
          type="success"
          icon="el-icon-edit"
          @click="addTagShow"
          >新增目录</el-button
        >
         <el-button class="editButton fr subjectBtn" v-if="$route.query.id&&$route.query.name" @click="$router.back()" >←返回学科</el-button>
      </el-form>
      <!-- 顶部搜索清除新增模块结束 -->
      <!-- 信息提示多少条 -->
<el-alert
    :title="text"
    type="info"
    show-icon
    :closable="false"
    >
  </el-alert>
<!-- 信息提示多少条结束 -->
<!-- 表格框开始 -->
<el-table
    :data="subjectTagsItem"
    stripe
    style="width: 100%">
    <el-table-column type="index" label="序号" :index="addIndex"></el-table-column>
    <el-table-column
      prop="subjectName"
      label="所属学科"
      width="180">
    </el-table-column>
    <el-table-column
      prop="tagName"
      label="标签名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="username"
      label="创建者">
    </el-table-column>
    <el-table-column
      prop=""
      label="创建日期">
<template slot-scope="scope">
{{scope.row.addDate|parseTimeByString("{y}-{m}-{d}")}}
</template>
    </el-table-column>

    <el-table-column
      prop=""
      label="状态"
      >
    <template slot-scope="scope">
{{scope.row.state===1?'已启用':'已禁用'}}
    </template>

    </el-table-column>
    <el-table-column
      prop="state"
      label="操作">
      <template slot-scope="scope">
       <button class="editButton" :class="{active:true}" @click="changeStates(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</button>
      <button class="edit editButton" :class="{active:scope.row.state===0}" :disabled="scope.row.state===1?true:false" @click="editorTag(scope.row)">修改</button>
      <button :class="{active:scope.row.state===0}" :disabled="scope.row.state===1?true:false" class="editButton" @click="DeleteVisible(scope.row.id)">删除</button>
      </template>

    </el-table-column>
  </el-table>
  <!-- 表格框结束 -->
  <!-- 页码部分开始 -->
 <el-pagination

      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="subjectInfo.page"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="subjectInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper,total"
      :total="total"
      class="fr"
      >
    </el-pagination>
    <!-- 页码部分结束 -->
    </el-card>
    <!-- 新增目录弹框 -->
<el-dialog title="新增目录" :visible.sync="addTagVisible"  width="30%"  @close="clearInfo">
  <!-- 子组件部分 -->
  <add-tag @close="addTagVisible=false" :subject="subject" @updateTag="updateInfo" ref="clearForm"></add-tag>
</el-dialog>
<!-- 新增目录弹框结束 -->

<!-- 修改目录弹框 -->
<el-dialog title="修改目录" :visible.sync="editorTagVisible"  width="30%"
>
<el-form :model="editorInfo" label-width="100px" :rules="rules" ref="ruleForm">
<!-- 当不是从学科进来的才会显示这个选择框 -->
    <el-form-item label="所属学科"  v-if="!$route.query.id&&!$route.query.name" >
      <el-select v-model="editorInfo.subjectID" placeholder="请选择活动区域">
        <el-option v-for="(item) in subject" :key="item.value"   :label="item.label" :value="item.value"></el-option>

      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" prop="tagName">
    <el-input v-model="editorInfo.tagName" style="width:218px"></el-input>
  </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer" >
    <el-button @click="editorTagVisible=false">取 消</el-button>
    <el-button type="primary" @click="dialogEditorVisible">确 定</el-button>
  </div>
</el-dialog>
<!-- 修改目录弹框结束 -->

  </div>
</template>

<script>
import { list, update, remove, changeState } from '@/api/hmmm/tags'
// import { list as subjectList } from '@/api/hmmm/subjects'
import { simple } from '@/api/hmmm/subjects'
import addTag from '@/module-hmmm/components/tags-add'
// import { parseTime } from '@/filters/index'
export default {
  name: '',
  components: { addTag },
  data () {
    return {
      value: '',
      total: 0,
      // 初始化,搜素,页面等需要的参数对象
      subjectInfo: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: undefined

      },
      // 渲染表格的数组
      subjectTagsItem: [],
      // 控制新增弹框的数据
      addTagVisible: false,
      // 点击修改新增等需要渲染的学科数组
      subject: [],
      editorTagVisible: false,
      // 修改需要的数据
      editorInfo: {
        subjectID: '',
        tagName: '',
        id: ''
      },
      // 定义的点击删除当前项的ID
      deleteTag: {
        id: ''
      },
      rules: {
        tagName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }]
      }

    }
  },
  created () {
    // 页面初始化时判断是从学科进来的还是直接点击的标签进来 的判断
    if (this.$route.query.id && this.$route.query.name) {
      this.subjectGetIn()
    } else {
      this.getSubjectInfo()
    }
  },

  methods: {
    // 序号自增方法
    addIndex (index) {
      return (this.subjectInfo.page - 1) * this.subjectInfo.pagesize + index + 1
    },

    // 学科进来的显示
    async subjectGetIn () {
      if (this.$route.query.id && this.$route.query.name) {
        // 学科进来的额渲染函数
        this.getInList()
      }
    },
    // 页面渲染数据函数
    async getSubjectInfo () {
      // 请求页面渲染的数据接口
      const { data } = await list(this.subjectInfo)
      // 把数据复制给页面渲染数组
      this.subjectTagsItem = data.items
      // 页面那的数据总量的
      this.total = data.counts
      console.log(data)
    },
    // 页面显示几条的数据函数
    handleSizeChange (size) {
      this.subjectInfo.pagesize = size
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // 第几页函数
    handleCurrentChange (num) {
      this.subjectInfo.page = num
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // 搜索渲染
    searchSubjectInfo () {
      if (this.subjectInfo.tagName === '' && this.subjectInfo.state === '') return
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // 搜素旁边的清除清空
    deleteSearch () {
      this.$refs.filterForm.resetFields()
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
      // this.subjectInfo.tagName = ''
      // this.subjectInfo.state = ''
    },
    // 新增目录弹出框渲染
    async addTagShow () {
      this.addTagVisible = true
      // 请求得学科列表
      const { data: res } = await simple()
      console.log(res)
      this.subject = res
    },
    // 新增弹出框数据渲染
    updateInfo () {
      if (!this.$route.query.id && !this.$route.query.name) {
        this.subjectInfo.page = 1
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // 修改目录弹出框渲染
    async editorTag (data) {
      // 当打开弹出框显示默认的数据
      this.editorTagVisible = true
      this.editorInfo.subjectID = data.subjectID
      this.editorInfo.id = data.id
      this.editorInfo.tagName = data.tagName
      // 请求学科的列表
      const { data: res } = await simple()
      console.log(res)
      this.subject = res
      this.editorTagVisible = true
    },
    // 修改弹出框数据渲染
    async dialogEditorVisible () {
      this.editorTagVisible = false
      // 修改后数据发起请求
      await update(
        this.editorInfo
      )
      this.$message({
        type: 'success',
        message: '修改成功!'
      })
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // clearEditor () {
    //   this.editorInfo.subjectID = ''
    //   this.editorInfo.tagName = ''
    // },
    // 删除目录
    async DeleteVisible (id) {
      this.deleteTag.id = id

      this.$confirm('此操作将永久删除此目录,是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async (action) => {
          await remove(this.deleteTag)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          if (!this.$route.query.id && !this.$route.query.name) {
            this.getSubjectInfo()
          } else {
            this.getInList()
          }
        })
        .catch(() => {})
    },
    // 点击启用按钮修改状态是启用还是禁用
    async changeStates (data) {
      await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      })
      this.$message({
        type: 'success',
        message: '修改状态成功!'
      })
      if (!this.$route.query.id && !this.$route.query.name) {
        this.getSubjectInfo()
      } else {
        this.getInList()
      }
    },
    // 当从学科管理进来的时候的页面数据渲染函数
    async getInList () {
      this.subjectInfo.subjectID = +this.$route.query.id
      const { data } = await list(this.subjectInfo)
      // console.log(data.items)
      // 筛选传过来的ID与获取数据的数组中的subjectID,如果是一样的就显示到页面当中
      // this.subjectTagsItem = data.items.filter(item => {
      //   console.log(item)
      //   // console.log(this.$route.query.id)
      //   return item.subjectID === +this.$route.query.id
      // })
      this.subjectTagsItem = data.items
      this.total = data.counts
      console.log(this.subjectTagsItem)
    },
    // 通知子组件的新增弹出框点击×号关闭清空输入框的内容
    clearInfo () {
      this.$refs.clearForm.clearform()
    }
  },
  computed: {
    text () {
      return `数据一共有${this.total}条`
    }
  }
}
</script>

<style lang="less" scoped>
.el-status{
  margin-left: 30px;
}
/deep/.is-link{
  color: #000 ;
}
.edit{
  margin: 0 10px;
}
.active{
  color: #07B8FA;
}
.editButton{
  border: none;
background-color: transparent;
outline: none;
}
.subjectBtn{
  padding: 10px 0;
  color: #07B8FA;
}
</style>
